﻿#include "ManTree.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/Data/Stat.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Enums.h"
#include "../MapObject.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    ManTree::ManTree(MonsterInfo *info) : ZumaMonster(info)
    {
    }

    bool ManTree::InAttackRange()
    {
        if (getTarget()->getCurrentMap() != getCurrentMap())
        {
            return false;
        }
        if (getTarget()->getCurrentLocation() == getCurrentLocation())
        {
            return false;
        }

        int x = std::abs(getTarget()->getCurrentLocation()->X - getCurrentLocation()->X);
        int y = std::abs(getTarget()->getCurrentLocation()->Y - getCurrentLocation()->Y);

        if (x > 2 || y > 2)
        {
            return false;
        }

        return (x == 0) || (y == 0) || (x == y);
    }

    void ManTree::Attack()
    {
        ShockTime = 0;

        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));
        bool ranged = getCurrentLocation() == getTarget()->getCurrentLocation() || !Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), 1);

        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);

        AttackTime = getEnvir()->getTime() + AttackSpeed;
        ActionTime = getEnvir()->getTime() + 300;

        if (!ranged)
        {
            if (getEnvir()->Random->Next(3) > 0)
            {
                S::ObjectAttack *tempVar = new S::ObjectAttack();
                tempVar->ObjectID = ObjectID;
                tempVar->Direction = getDirection();
                tempVar->Location = getCurrentLocation();
                Broadcast(tempVar);
                Attack1(1);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            }
            else
            {
                S::ObjectAttack *tempVar2 = new S::ObjectAttack();
                tempVar2->ObjectID = ObjectID;
                tempVar2->Direction = getDirection();
                tempVar2->Location = getCurrentLocation();
                tempVar2->Type = 1;
                Broadcast(tempVar2);
                Attack2(1);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
            }

        }
        else
        {
            S::ObjectRangeAttack *tempVar3 = new S::ObjectRangeAttack();
            tempVar3->ObjectID = ObjectID;
            tempVar3->Direction = getDirection();
            tempVar3->Location = getCurrentLocation();
            tempVar3->TargetID = getTarget()->ObjectID;
            Broadcast(tempVar3);
            AttackRange1(1);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
        }


        if (getTarget()->Dead)
        {
            FindTarget();
        }
    }

    void ManTree::Attack1(int distance)
    {
        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
        if (damage == 0)
        {
            return;
        }
        getTarget()->Attacked(this, damage, DefenceType::ACAgility);
    }

    void ManTree::Attack2(int distance)
    {
        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
        if (damage == 0)
        {
            return;
        }
        getTarget()->Attacked(this, damage, DefenceType::ACAgility);
    }

    void ManTree::AttackRange1(int distance)
    {
        std::vector<MapObject*> targets = FindAllTargets(2, getTarget()->getCurrentLocation());
        if (targets.empty())
        {
            return;
        }

        for (int i = 0; i < targets.size(); i++)
        {
            int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC] * 2);
            if (damage == 0)
            {
                return;
            }

            if (targets[i]->Attacked(this, damage, DefenceType::AC) <= 0)
            {
                return;
            }
        }
    }

    void ManTree::ProcessTarget()
    {
        if (getTarget() == nullptr)
        {
            return;
        }

        if (InAttackRange() && getCanAttack())
        {
            Attack();
            if (getTarget()->Dead)
            {
                FindTarget();
            }

            return;
        }

        if (getEnvir()->getTime() < ShockTime)
        {
            setTarget(nullptr);
            return;
        }

        MoveTo(getTarget()->getCurrentLocation());
    }
}
